// // import { GameData_DanCiPeiDui } from "../../../../Script/Game/DanCiPeiDui/GameData_DanCiPeiDui";

import { _decorator, AudioClip, Component, director } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import YangLeGeYang_GameFunction from "./YangLeGeYang_GameFunction";
import YangLeGeYang_LevelManager from "./YangLeGeYang_LevelManager";
import FailPanel from "../../../../Script/UI/FailPanel";
import MainPanel from "../../../../Script/UI/MainPanel";
import WinPanel from "../../../../Script/UI/WinPanel";
import YangLeGeYang_CardManager from "./YangLeGeYang_CardManager";
import SSS_TimerManager from "../../../../Script/Manager/SSS_TimerManager";
import UI from "../../../../Script/Manager/UI";
import YangLeGeYang_GamePanel from "../UI/YangLeGeYang_GamePanel";
import { YangLeGeYang_GameStorage } from "../Data/YangLeGeYang_GameStorage";
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';


@ccclass('YangLeGeYangGameManager')
export default class YangLeGeYang_GameManager extends Component {
    public static Instance: YangLeGeYang_GameManager;
    onLoad() {
        YangLeGeYang_GameManager.Instance = this;

        UI.Instance.ResetUINode();

        this.Init();
    }
    Init() {
        YangLeGeYang_GameStorage.initData();

        // MainPanel.Instance.ShowOn();
        // MainPanel.Instance.init();
    }
    protected start(): void {
        YangLeGeYang_GameManager.Instance.StartGame();
    }
    async StartGame(level = 1) {
        // TransitionsPanel.Instance.ShowOn();

        YangLeGeYang_GameManager.Instance.ClearGame();
        await YangLeGeYang_GameData.InitLevelData(level);

        YangLeGeYang_LevelManager.Instance.Init();


        YangLeGeYang_GamePanel.Instance.ShowOn();
    }
    //    // 退出游戏
    ExitGame() {
        console.log(`退出`);

        YangLeGeYang_GameManager.Instance.ClearGame();
        YangLeGeYang_GamePanel.Instance.ShowOff();
        MainPanel.Instance.ShowOn();
    }
    //    // 过关
    PassLevel() {
        console.log(`过关`);

        YangLeGeYang_GameData.CurrentLevel++;
        YangLeGeYang_GameManager.Instance.StartGame();

        // if (DanLeGeCi_GameData.CurrentLevel === 0) {
        //     GameManager.Instance.ClearGame();
        //     DanLeGeCi_GameData.InitLevelData(1);
        //     LevelManager.Instance.Init();
        //     GamePanel.Instance.Refresh();
        //     TransitionsPanel.Instance.ShowOn();
        // }
        // else {
        //     GameManager.Instance.GameWin();
        // }
    }

    上一关() {
        console.log(`上一关`);
        // YangLeGeYang_GameData.CurrentLevelIndex++;
        YangLeGeYang_GameData.Set_CurrentLevelIndex(YangLeGeYang_GameData.Get_CurrentLevelIndex() - 1);

        let levelList = YangLeGeYang_GameData.Get_LevelList();
        // DanLeGeCi_GameData.CurrentLevelData = levelList.wordList[DanLeGeCi_GameData.CurrentLevelIndex];
        YangLeGeYang_GameData.CurrentLevelData = YangLeGeYang_GameData.GetCurrentLevelData();

        director.loadScene(SSS_Consts.SceneName.YangLeGeYang_Game_Scene);
    }

    下一关() {
        console.log(`下一关`);
        // YangLeGeYang_GameData.CurrentLevelIndex++;
        YangLeGeYang_GameData.Set_CurrentLevelIndex(YangLeGeYang_GameData.Get_CurrentLevelIndex() + 1);

        let levelList = YangLeGeYang_GameData.Get_LevelList();
        // DanLeGeCi_GameData.CurrentLevelData = levelList.wordList[DanLeGeCi_GameData.CurrentLevelIndex];
        YangLeGeYang_GameData.CurrentLevelData = YangLeGeYang_GameData.GetCurrentLevelData();

        director.loadScene(SSS_Consts.SceneName.YangLeGeYang_Game_Scene);
    }

    //    // 胜利
    GameWin() {
        UI.Instance.CreatePanel(`UI/WinPanel`, WinPanel);

        let audio_root = `audio/level complete`;
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });

        // YangLeGeYang_GameManager.Instance.ClearGame();
    }

    //    // 失败
    async GameFail() {
        await new Promise(res => SSS_TimerManager.Instance.scheduleOnce(res, 0.5));
        UI.Instance.CreatePanel(`UI/FailPanel`, FailPanel);

        // let audio_root = `audio/level failed`;
        // bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
        //     AudioManager.Instance.playSFX(res, false);
        // });
    }

    //    // 复活
    Reborn() {
        YangLeGeYang_GameData.isDead = false;
        YangLeGeYang_GameData.LevelRebornNumber--;
        // GameStorage.data.reborn_times_all++;
        // GameStorage.saveData();
        YangLeGeYang_GameFunction.RemoveOperatePanel();
    }

    //    // 
    Replay() {
        this.ClearGame();
        director.loadScene(SSS_Consts.SceneName.YangLeGeYang_Game_Scene);
    }
    //    // 清除关卡
    ClearGame() {
        YangLeGeYang_CardManager.Instance.CardList = [];
        YangLeGeYang_GameData.ClearData();
        YangLeGeYang_LevelManager.Instance.ClearCards();
    }
    onDestroy(): void {
        // 停止所有正在播放的音效
        // audioEngine.stopAllEffects();
    }
}

// /** 调试 */
window["YangLeGeYang_GameManager"] = YangLeGeYang_GameManager;